<?php

/**
 * IG_Controller_Action_Helper_Jstree
 *
 * @author mmxie
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Jstree.php 929 2011-10-27 07:52:50Z mmxie $
 */
class IG_Controller_Action_Helper_Jstree extends Zend_Controller_Action_Helper_Abstract
{
    public function direct($rows, $rootId, $metadata = false)
    {
        $data = array($rootId => array('children' => array()));
        foreach ($rows as $row) {
            $data[$row['id']] = (isset($data[$row['id']]) ?  $data[$row['id']] : array())
                              + array(
                'data' => array(
                    'title' => $row['title'],
                    'attr' => array(
                        'href'  => $row['url'],
                    ),
                ),
                'attr' => array(
                    'id'   => 'ig-jstree-' . $row['id'],
                    'rel'  => $row['type'],
                ),
            );
            if ($metadata) {
                $data[$row['id']]['metadata'] = $row;
            }
            $data[$row['parent_id']]['children'][] = & $data[$row['id']];
        }
        return $data[$rootId]['children'];
    }
}